package com.hotec.PROJECT_NAME.modules.sys.controller;

import com.hotec.PROJECT_NAME.modules.sys.entity.SysResource;
import com.hotec.PROJECT_NAME.component.basic.BaseController;
import com.hotec.PROJECT_NAME.component.basic.JsonContent;
import com.hotec.PROJECT_NAME.modules.sys.service.SysResourceService;
import com.hotec.PROJECT_NAME.utils.UtilsTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author llb

 */
@RestController
@RequestMapping("sys/resource")
public class SysResourceController extends BaseController {
    @Autowired
    SysResourceService service;

    /**
     * 获取用户的导航栏
     *
     * @return
     */
    @PostMapping("navigate")
    public JsonContent navigate() {
        return this.success(service.navigate());
    }


    /**
     * 根据主键查询对象
     *
     * @param id
     * @return
     */
    @GetMapping("get")
    public JsonContent get(String id) {
        return this.success(service.get(id));
    }


    /**
     * 资源管理查询，不允许分页查询，条件查询
     *
     * @return
     */
    @PostMapping("queryAll")
    public JsonContent queryAll() {
        SysResource queryEntity = new SysResource().setTenantCode(getCurrentReqClient());

        return success(new UtilsTreeBuilder<>(SysResource.class).listToTree(service.queryAll(queryEntity, "sort_ * 1 asc")));
    }

    /**
     * 根据对象列表逻辑删除
     *
     * @param entityList
     * @return
     */
    @PostMapping("delete")
    public JsonContent delete(@RequestBody List<SysResource> entityList) {
        service.deleteById(entityList);
        return this.success().setMessage("删除成功！");
    }

    /**
     * 保存更新
     *
     * @return
     */
    @PostMapping("save")
    public JsonContent save(@Valid @RequestBody SysResource entity) {
        entity.setTenantCode(getCurrentReqClient());

        service.save(entity);

        return this.success().setMessage("操作成功！");
    }


    /**
     * 根据对象条件，查询单条数据
     *
     * @return
     */
    @PostMapping("getOne")
    public JsonContent getOne(@RequestBody SysResource entity) {
        return this.success(service.getOne(entity));
    }
}
